package com.lysj.admin.master;

import cn.hutool.cache.Cache;
import cn.hutool.core.util.StrUtil;
import com.lysj.admin.master.auth.service.AdminAuthentication;
import com.lysj.admin.master.common.CommonConstant;
import com.lysj.admin.master.common.enumeration.EnumInterface;
import com.lysj.admin.master.common.enumeration.EnumUtils;
import com.lysj.admin.master.common.spring.SpringContextUtil;
import com.lysj.admin.master.common.spring.base.BaseService;
import com.lysj.admin.master.common.spring.mpbase.MyService;
import com.lysj.admin.master.common.web.Resp;
import com.lysj.admin.master.common.web.RespUtil;
import com.lysj.admin.master.common.web.SelectItem;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author Created by zk on 2019-08-02 9:53
 * @description
 */
@RestController
@RequestMapping("/config/admin")
public class ConfigController {

    @Resource
    private AdminAuthentication adminAuthentication;
    @Resource
    private CacheManager cacheManager;

    /**
     * @author Created by zk on 2019/8/2 10:41
     * @Description 根据枚举名获取selectItem，注意，若存在名字相同则无法使用
     */
    @GetMapping("/config_enum/{enumName}")
    public Resp configEnum(@PathVariable String enumName) {
        Class<? extends EnumInterface> cls = CacheManager.CONFIG_UTIL.get(enumName.toLowerCase());
        List<SelectItem> selectItems = new ArrayList<>();
        if (cls != null) {
            selectItems = EnumUtils.createEnumSelectItem(cls);
        }
        return RespUtil.success(selectItems);
    }

    @GetMapping("/select_item/{entityName}")
    public Resp selectItem(@PathVariable String entityName) {
        MyService baseService = (MyService) SpringContextUtil.getBean(StrUtil.lowerFirst(entityName) + "Service");
        if(baseService == null){
            return RespUtil.error(Resp.ResStatus.PARAM_ERROR,"表名有误");
        }
        return RespUtil.success(baseService.selectItem());
    }


    @PostMapping("/init_permission_cache")
    public Resp initPermissionCache(String password){
        if(CommonConstant.INTERFACE_PASSWORD.equals(password)){
            cacheManager.initAdminPermissionRoleCache();
            return RespUtil.success("权限缓存初始化成功");
        }
        return RespUtil.error(Resp.ResStatus.PARAM_ERROR,"密码错误");
    }

}
